import random

from com.xunai.saas.example.Common import CommonRequest


class FaceApi(CommonRequest):
    """
    面诊
    """

    def consultation(self, faceImgPath, faceLeftImgPath, faceRightImgPath):
        """
        初步分析
        :param faceImgPath: 面部图片网络地址
        :param faceLeftImgPath: 面部左侧图片网络地址
        :param faceRightImgPath: 	面部右侧图片网络地址
        :return:
        """
        body = {
            "faceImgPath": faceImgPath,
            "faceLeftImgPath": faceLeftImgPath,
            "faceRightImgPath": faceRightImgPath
        }
        rs = super().post("/xunai-saas/saasApi/v1/face/faceAnalysis", body)
        print("初步分析结果", rs.json())
        self.first(rs.json()['data']['faceId'])

    def first(self, faceQSnowId):
        """
        初始问题，提交年龄和性别
        :param faceQSnowId:
        :return:
        """
        body = {
            "answer": [{"answerCode": 30, "subjectId": 999}, {"answerCode": 0, "subjectId": 1000}],
            "faceId": faceQSnowId
        }
        rs = super().post("/xunai-saas/saasApi/v1/face/faceAnswerAnalysis", body)
        print(rs.json())
        self.next(rs.json()['data'])

    def next(self, data):
        """
        循环答题
        :param data:
        :return:
        """
        body = {
            "answer": [
                {"subjectId": data['subject'][0]['subjectId'],
                 "answerCode": data['subject'][0]['answer'][
                     random.randint(0, len(data['subject'][0]['answer']) - 1)]['answerCode']}
            ],
            "faceId": data['faceId']
        }
        rs = super().post("/xunai-saas/saasApi/v1/face/faceAnswerAnalysis", body)
        print(rs.json())
        if rs.json()['data']['isFinalResult'] == 0:
            self.next(rs.json()['data'])
        else:
            print("最终结果：", rs.json())


if __name__ == "__main__":
    FaceApi().consultation("正脸", "左脸", "右脸")
